import { _decorator, Button, instantiate, Label, Node, Sprite, SpriteAtlas, tween, UITransform, v3 } from 'cc';
import { UIComponent } from '../../../Framework/Scripts/Managers/UIComponent';
import { ResManager } from '../../../Framework/Scripts/Managers/ResManager';
import { AbNames, EventMsgKes } from '../Config/ResConfig';
import { Respones, RewardType } from '../Config/GameCfg';
import { GetNewPlayerRes, IReward } from '../../AssetPackage/Props/Hall/GetUserReward';
const { ccclass} = _decorator;

/**
 * 新手礼包
 */
@ccclass('NewPlayer_Ctr')
export class NewPlayer_Ctr extends UIComponent {
    bg: Node;
    isClick: Boolean;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();
        let root = this.ViewNode('root');
        this.AddOpenAni(root);

        this.bg = this.ViewNode('root/Sprite/mask/bg1');

        this.AddButtonListener('root/btnClose',this,this.onGetRewardClick);
        this.AddButtonListener('root/btnGet',this,this.onGetRewardClick)
    }

    private InitData(){
        this.isClick = true;
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGetNewPlayerRes,this.onGetNewPlayerRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgGetNewPlayerRes,this.onGetNewPlayerRes,this);
    }

    //点击领取奖励
    private onGetRewardClick(btn: Button){
        if(this.isClick == false){
            return;
        }
        this.isClick = false;
        globalThis.gameMgr._GameHallMsg.GetNewPlayerReq();
    }

    public setData(data: IReward[]) {
        let startY = 97.5;
        let offset = 26;
        let parent = this.bg.getChildByName('bg2');
        if (data && data.length > 0) {
            let atals = ResManager.inst.getAsset(AbNames.GameHall, 'Atals/GameHall', SpriteAtlas) as SpriteAtlas;
            let item = parent.children[0];
            for (let i = 0; i < data.length - 1; i++) {
                let temp = instantiate(item);
                parent.addChild(temp);
            }

            let children = parent.children;
            for (let i = 0; i < children.length; i++) {
                let node = children[i];
                const cfg: IReward = data[i];
                // console.log('========children[i]============',node.name);
                node.setPosition(v3(0,startY - i*(node.getComponent(UITransform).height + offset),0));
                let spIcon = node.getComponentInChildren(Sprite);
                let lab = node.getComponentInChildren(Label);

                let sf = null;
                if (cfg.rewardType === RewardType.TYpe_QiuNum) {
                    sf = atals.getSpriteFrame('NewPlayer_icon');
                } else if (cfg.rewardType === RewardType.Type_Quan) {
                    sf = atals.getSpriteFrame('NewPlayer_icon3');
                }
                if (sf) {
                    spIcon.spriteFrame = sf;
                }
                if (cfg.rewardNum) {
                    lab.string = `x${cfg.rewardNum}`;
                }
            }

            tween()
                .target(this.bg)
                .hide()
                .by(0, { position: v3(0, -432, 0) })
                .show()
                .by(0.5, { position: v3(0, 432, 0) })
                .start()
        }
    }

    private onGetNewPlayerRes(data:GetNewPlayerRes){
        this.isClick = true;
        if(this.hasProInObj(data,'code')){
            let code = parseInt(data.code);
            if(code == Respones.OK){
                this.node.destroy();
            }else{
                this.node.destroy();
            }
        }
    }
}


